package com.aidex.web.controller.system;

import com.aidex.biz.domain.AccountLog;
import com.aidex.biz.service.AccountLogService;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.domain.entity.Company;
import com.aidex.common.core.page.PageDomain;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.SecurityUtils;
import com.aidex.common.utils.poi.ExcelUtil;
import com.aidex.system.service.CompanyService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;


/**
 * 公司信息Controller
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2022-03-04
 */
@RestController
@RequestMapping("/biz/bizCompany")
public class SysCompanyController extends BaseController {
    @Autowired
    private CompanyService companyService;
    @Autowired
    private AccountLogService accountLogService;

    /**
     * 查询公司信息列表
     */
    // @PreAuthorize("@ss.hasPermi('biz:bizCompany:list')")
    @GetMapping("/list")
    public R<PageInfo> list(Company company, HttpServletRequest request, HttpServletResponse response) {
        company.setPage(new PageDomain(request, response));
        return R.data(companyService.findPage(company));
    }

    /**
     * 获取公司信息详细信息
     */
    // @PreAuthorize("@ss.hasPermi('biz:bizCompany:query')")
    @GetMapping(value = "/{id}")
    public R<Company> detail(@PathVariable("id") String id) {
        if ("null".equals(id)) {
            id = SecurityUtils.getLoginUser().getUser().getCompanyId();
        }
        return R.data(companyService.get(id));
    }

    /**
     * 新增公司信息
     */
    // @PreAuthorize("@ss.hasPermi('biz:bizCompany:add')")
    @Log(title = "公司信息", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated Company company) {
        company.setBalance(new BigDecimal(0));
        return R.status(companyService.save(company));
    }

    /**
     * 修改公司信息
     */
    // @PreAuthorize("@ss.hasPermi('biz:bizCompany:edit')")
    @Log(title = "公司信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated Company company) {
        // company.setBalance(new BigDecimal(0));
        return R.status(companyService.save(company));
    }


    /**
     * 删除公司信息
     */
    // @PreAuthorize("@ss.hasPermi('biz:bizCompany:remove')")
    @Log(title = "公司信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(companyService.deleteCompanyByIds(ids));
    }

    /**
     * 获取最大编号
     */
    //@PreAuthorize("@ss.hasPermi('biz:bizCompany:query')")
    @GetMapping("/findMaxSort")
    public R findMaxSort() {
        return R.data(companyService.findMaxSort(new Company()));
    }

    /**
     * 导出公司信息列表
     */
    // @PreAuthorize("@ss.hasPermi('biz:bizCompany:export')")
    @Log(title = "公司信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(Company company) {
        List<Company> list = companyService.findList(company);
        ExcelUtil<Company> util = new ExcelUtil<Company>(Company.class);
        return util.exportExcel(list, "公司信息数据");
    }

    @PreAuthorize("@ss.hasPermi('biz:bizCompany:rechange')")
    @Log(title = "公司账户充值", businessType = BusinessType.UPDATE)
    @PostMapping("/rechange")
    public R rechange(@RequestBody @Validated Company company) {
        try {
            Company oldcompany = companyService.get(company.getId());
            oldcompany.setBalance(oldcompany.getBalance().add(company.getRechangeMoney()));
            companyService.save(oldcompany);
            AccountLog accountLog = new AccountLog();
            accountLog.setCompanyId(oldcompany.getId());
            accountLog.setCompanyName(oldcompany.getCompanyName());
            accountLog.setType("充值");
            accountLog.setAmount(company.getRechangeMoney());
            accountLog.setEndAmount(oldcompany.getBalance());
            accountLog.setStartAmount(oldcompany.getBalance().subtract(accountLog.getAmount()));
            accountLog.setCreateTime(new Date());
            accountLogService.save(accountLog);
            return R.success();
        } catch (Exception exception) {
            return R.fail("充值失败", exception.getMessage());
        }
    }

    /**
     * 查询所有的公司信息
     *
     * @param company
     * @return
     */
    @GetMapping("/findcompanyAllList")
    public R findcompanyAllList(Company company) {
        return R.data(companyService.findList(company));
    }
}
